package cn.hxzy.student.service.impl;

import cn.hxzy.common.domain.TaskManager;
import cn.hxzy.common.domain.vo.TaskManagerVo;
import cn.hxzy.student.mapper.TaskManagerMapper;
import cn.hxzy.student.service.TaskManagerService;
import cn.hxzy.common.util.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

@Service
public class TaskManagerServiceImpl extends ServiceImpl<TaskManagerMapper, TaskManager> implements TaskManagerService {
    @Autowired
    private TaskManagerMapper taskManagerMapper;
    @Override
    public boolean updateTaskManger(TaskManager taskManager) {
//        LambdaQueryWrapper<TaskManager> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(TaskManager::getTaskId, taskManager.getTaskId());
//        queryWrapper.eq(TaskManager::getStudentId, taskManager.getStudentId());


        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("task_id", taskManager.getTaskId());
        queryWrapper1.eq("student_id", taskManager.getStudentId());

        int update = taskManagerMapper.update(taskManager, queryWrapper1);
        if (update > 0){
            return true;
        }
        return false;
    }

    /**
     * 根据作业编号与学生编号查询作业
     * @param taskId
     * @return
     */
    @Override
    public TaskManager getTaskMangerByTaskId(Integer taskId) {
        Long userId = ThreadLocalUtil.getUser().getUserId();
//```````````````````````````````````````````````````````````````````````````````        LambdaQueryWrapper<TaskManager> queryWrapper = new LambdaQueryWrapper<>();
//        // 根据作业编号与学生编号查询作业
//        queryWrapper.eq(TaskManager::getTaskId, taskId);
//        queryWrapper.eq(TaskManager::getStudentId, userId.intValue());```````````````````````````````````````````````````````````````````````````````

        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("task_id", taskId);
        wrapper.eq("student_id", userId.intValue());
        return taskManagerMapper.selectOne(wrapper);
    }
    @Override
    public List<TaskManagerVo> GetTaskManagerList() {
        Integer teacherId= Math.toIntExact(ThreadLocalUtil.getUser().getUserId());
        List<TaskManagerVo> taskManagers = taskManagerMapper.GetTaskManagerList(teacherId);
        for (TaskManagerVo taskManagerVo:taskManagers){
            if(!StringUtils.isEmpty(taskManagerVo.getImageUrl())){
                String imageUrl = taskManagerVo.getImageUrl();
                List<String> split = Arrays.asList(imageUrl.split(","));
                taskManagerVo.setTaskImgUrl(split);
            }else{
                taskManagerVo.setTaskImgUrl(null);
            }
        }
        return taskManagers;
    }

    @Override
    public List<TaskManagerVo> getClassIdByList( Integer classId) {
        Integer teacherId= Math.toIntExact(ThreadLocalUtil.getUser().getUserId());
        List<TaskManagerVo> classIdByList = taskManagerMapper.getClassIdByList(teacherId, classId);
        return classIdByList;
    }


    @Override
    public Integer count(Integer userId, Integer taskId) {
        return taskManagerMapper.countByStudentIdAndTaskId(userId, taskId);
    }


}
